#include "Thread.hpp"
using namespace ThreadModule;

int g_tickets = 2000;
void route(int &tickets)
{
    while (true)
    {
        if (tickets > 0)
        {
            usleep(1000);
            printf("get tickets: %d\n", tickets);
            tickets--;
        }
        else
        {
            break;
        }
    }
}
const int num = 5;
int main()
{
    std::vector<Thread<int>> threads;

    for (int i = 0; i < num; i++)
    {
        std::string name = "thread-" + std::to_string(i + 1);
        threads.emplace_back(route, g_tickets, name);
    }

    for (auto &thread : threads)
    {
        thread.Start();
    }

    for (auto &thread : threads)
    {
        thread.Join();
        std::cout << "wait thread  done,thread is : " << thread.name() << std::endl;
    }

    return 0;
}

/*
void print(int &cnt)
{
    while(cnt--)
    {
        std::cout << "hello I am myself thread,cnt: " << cnt << std::endl;
        sleep(1);
    }
}

const int num = 10;

int main()
{
    std::vector<Thread<int>> threads;
    // 1.创建一批线程
    for(int i = 0;i < num;i++)
    {
        std::string name = "thread-" + std::to_string(i + 1);
        threads.emplace_back(print,10,name);
    }
    // 2.启动一批线程
    for(auto &thread : threads)
    {
        thread.Start();
    }
    // 3.等待一批线程
    for(auto &thread : threads)
    {
        thread.Join();
        std::cout << "wait thread  done,thread is : " << thread.name() << std::endl;
    }

    Thread<int> t1(print,10);
    t1.Start();
    std::cout << "name: " << t1.name() << std::endl;
    t1.Join();

    return 0;
}
*/

/*
std::string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%lx", tid);
    return id;
}
__thread uint64_t starttime = 100;
__thread int tid = 0;
int g_val = 100;
// 线程是可以分离的: 默认线程是joinable的。
// 如果我们main thread不关心新线程的执行信息，我们可以将新线程设置为分离状态:
// 你是如何理解线程分离的呢？底层依旧属于同一个进程！只是不需要等待了
// 一般都希望mainthread 是最后一个退出的，无论是否是join、detach
void *threadrun1(void *args)
{
    starttime = time(nullptr);
    // pthread_detach(pthread_self());
    std::string name = static_cast<const char *>(args);

    while(true)
    {
        sleep(1);
        printf("%s,g_val:%lu,&g_val:%p\n",name.c_str(),starttime,&starttime);
    }
    return nullptr;
}
void *threadrun2(void *args)
{
    sleep(5);
    starttime = time(nullptr);
    // pthread_detach(pthread_self());
    std::string name = static_cast<const char *>(args);

    while(true)
    {
        sleep(1);
        printf("%s,g_val:%lu,&g_val:%p\n",name.c_str(),starttime,&starttime);
    }
    return nullptr;
}
int main()
{
    pthread_t tid1;
    pthread_t tid2;
    pthread_create(&tid1, nullptr, threadrun1, (void *)"thread 1");
    pthread_create(&tid1, nullptr, threadrun2, (void *)"thread 2");

    pthread_join(tid1, nullptr);
    pthread_join(tid2, nullptr);
    pthread_detach(tid);

    std::cout << "new tid: " << tid << ", hex tid1: " << ToHex(tid) << std::endl;
    std::cout << "main tid: " << pthread_self() << ", hex tid: " << ToHex(pthread_self()) << std::endl;

    int cnt = 5;
    while (true)
    {
        if (!(cnt--))
            break;
        std::cout << "I am a main thread ..." << getpid() << std::endl;
        sleep(1);
    }
    std::cout << "main thread wait block" << std::endl;
    int n = pthread_join(tid, nullptr);
    std::cout << "main thread wait return: " << n << " : " << strerror(n) << std::endl;

    return 0;
}
*/

/* // C++对原生线程库进行的封装
#include <thread>
void threadrun(int num)
{
    while(num)
    {
        std::cout << "I am a thread,num: " << num << std::endl;
        sleep(1);
    }
}
int main()
{
    std::thread t1(threadrun,10);
    std::thread t2(threadrun,10);
    std::thread t3(threadrun,10);
    std::thread t4(threadrun,10);
    std::thread t5(threadrun,10);

    while(true)
    {
        std::cout << "I am a main thread" << std::endl;
        sleep(1);
    }

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();

    return 0;
}
*/